package com.xuanwei.admin.controller;

import com.xuanwei.core.annos.Contain;
import com.xuanwei.core.annos.Positive;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwHelpContentService;
import com.xuanwei.core.service.IXwHelpService;
import com.xuanwei.dao.entity.XwHelp;
import com.xuanwei.dao.entity.XwHelpContent;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/8/28 11:02
 **/
@RestController
@RequestMapping("api/help")
@Validated
public class HelpController extends BasicController {

    private IXwHelpService helpService;

    private IXwHelpContentService helpContentService;

    public HelpController(IXwHelpService helpService,
                          IXwHelpContentService helpContentService) {
        this.helpService = helpService;
        this.helpContentService = helpContentService;
    }

    @RequestMapping(value = "addHelp.do", method = RequestMethod.POST)
    public ResultVO addHelp(Integer id,
                            @NotBlank(message = "问题不你为空") String title,
                            @Positive(containZero = true, message = "排序格式错误") Integer orderBy,
                            @Contain(values = {0, 1}, message = "显示状态格式错误") Integer showStatus) {
        XwHelp help = new XwHelp();
        help.setId(id);
        help.setTitle(title);
        help.setShowStatus(showStatus);
        help.setOrderBy(orderBy);
        helpService.addOrUpdate(help);
        return ResultVO.successResult("保存成功");
    }

    @RequestMapping(value = "listHelp.do", method = RequestMethod.GET)
    public ResultVO listHelp(Integer type) {
        List<XwHelp> all = helpService.findAll(type);

        return ResultVO.successResult(all);
    }

    @RequestMapping(value = "updateStatusHelp.do", method = RequestMethod.GET)
    public ResultVO updateStatusHelp(@Positive(message = "ID格式错误") Integer id) {
        helpService.updateStatus(id);
        return ResultVO.successResult("修改成功");
    }

    @RequestMapping(value = "deleteHelp.do", method = RequestMethod.GET)
    public ResultVO deleteHelp(@Positive(message = "ID格式错误") Integer id) {
        helpService.getBaseMapper().deleteById(id);
        return ResultVO.successResult("删除成功");
    }

    @RequestMapping(value = "addHelpContent.do", method = RequestMethod.POST)
    public ResultVO addHelpContent(Integer id,
                                   @NotBlank(message = "问题不你为空") String content,
                                   @Positive(message = "helpId格式错误") Integer helpId,
                                   @Positive(containZero = true, message = "排序格式错误") Integer orderBy,
                                   @Contain(values = {0, 1}, message = "显示状态格式错误") Integer showStatus) {
        XwHelpContent helpContent = new XwHelpContent();
        helpContent.setId(id);
        helpContent.setContent(content);
        helpContent.setHelpId(helpId);
        helpContent.setShowStatus(showStatus);
        helpContent.setOrderBy(orderBy);
        helpContentService.addOrUpdate(helpContent);
        return ResultVO.successResult("保存成功");
    }

    @RequestMapping(value = "listHelpContent.do", method = RequestMethod.GET)
    public ResultVO listHelpContent(@Positive(message = "ID格式错误") Integer helpId, Integer type) {
        List<XwHelpContent> all = helpContentService.findAll(helpId, type);

        return ResultVO.successResult(all);
    }

    @RequestMapping(value = "updateStatusHelpContent.do", method = RequestMethod.GET)
    public ResultVO updateStatusHelpContent(@Positive(message = "ID格式错误") Integer id) {
        helpContentService.updateStatus(id);
        return ResultVO.successResult("修改成功");
    }

    @RequestMapping(value = "deleteHelpContent.do", method = RequestMethod.GET)
    public ResultVO deleteHelpContent(@Positive(message = "ID格式错误") Integer id) {
        helpContentService.getBaseMapper().deleteById(id);
        return ResultVO.successResult("删除成功");
    }
}
